<?php
	require_once 'C:\xampp\htdocs\PhotoAlbumOnline\autoLoadFunc.php';
	
	class DBMysql { 
		private $user; 
		private $pass; 
		private $dbhost; 
		private $dbname; 
		private $dbh; 
		
		
		private function __construct($user="root", $pass="", $dbhost="localhost", $dbname="photoalbum") { 
			$this->user = $user; 
			$this->pass = $pass; 
			$this->dbhost = $dbhost;
			$this->dbname = $dbname; 
		}

		public static function Instance() {
			static $instance = null;
			if($instance == null) {
				$instance =  new DBMysql();
			}
			return $instance;
		}
		
		private function connect() { 
			$this->dbh = mysql_pconnect($this->dbhost, $this->user, $this->pass)
				or die(mysql_error());
			mysql_select_db($this->dbname, $this->dbh)
				or die(mysql_error());
		}
		
		public function execute($query) { 
			if(!$this->dbh) { 
				$this->connect(); 
			}
			
			$result = mysql_query($query, $this->dbh)
				or die(mysql_error()); 
			
			$statement = new DBMysqlStatement($this->dbh, $query); 
			$statement->result = $result; 
			return $statement; 

		 } 
	
		function registerUser($user) {
			$username = $user->getUsername();
			$password = $user->getPassword();
			$birthDate = $user->getBirthDate();
			$email = $user->getEmail();
			
			$query =  "insert into users(username, password, birthDate, e_mail)
				values('$username', '$password', '$birthDate', '$email');";
			$statement = $this->execute($query);
			header("location:loginPageAfterReg.php");
		}
		
		function getUserInfo() {
			$username = $_SESSION['username'];
			$query = "SELECT username, password, birthDate, e_mail
				FROM users
				where username = '$username';";
			$statement = $this->execute($query);
			return $statement;
		}
		
		function getUserAlbums() {
			$username = $_SESSION['username'];
			$query = "Select albumId, name
					from albums
					join users on albums.userId = users.userId
					where username = '$username'";
			
			$albums = $this->execute($query);
			return $albums;
		}

		public function getCurrentUserId() {
			$username = $_SESSION['username'];
			$query = "Select userId
					from users
					where username = '$username'";
			$statement = $this->execute($query);
			$row = $statement->fetch_row();
			$id = $row[0];
			return $id;
		}
		
		function uploadImgToDB($image, $albumId) {
			$name = $image->getName();
			$descr = $image->getDescr();
			$query = "insert into images(name, description, albumId)
					values('$name','$descr','$albumId');";
			$result = $this->execute($query);
		}
		
		function addAlbumToDB($album) {
			$name = $album->getName();
			$descr = $album->getDescr();
			$userId = $album->getUserId();
			
			$query = "insert into albums(name, description, userId)
					values('$name', '$descr', $userId);";
			$result = $this->execute($query);
			echo "<p>Album created!</p>";		
		}
		
		function getLastAlbumId() {
			$query = "select albumId from albums
					order by albumId desc
					limit 1;";
			$result = $this->execute($query);
			$albumIds = $result->fetch_row();
			return $albumIds[0];
		}
	}
	
?>